package com.hkemmm.shop.service.impl;

import com.hkemmm.shop.dao.AddreasMapper;
import com.hkemmm.shop.dao.CommentMapper;
import com.hkemmm.shop.dao.OrderMapper;
import com.hkemmm.shop.dao.Order_detailMapper;
import com.hkemmm.shop.entity.Order;
import com.hkemmm.shop.entity.OrderInfo;
import com.hkemmm.shop.entity.Order_detail;
import com.hkemmm.shop.service.IOrderService;
import org.apache.ibatis.annotations.Param;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrderService implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private Order_detailMapper orderDetailMapper;
    @Autowired
    private AddreasMapper addreasMapper;
    @Autowired
    private CommentMapper commentMapper;

    /**
     * 查询订单数量
     *
     * @param uid    用户id
     * @param status 订单状态   0-未付款   1-已付款   2-已完成
     * @return
     */
    @Override
    public int countOrder(Integer uid, Integer status) {
        return orderMapper.countOrder(uid, status);
    }

    @Override
    public int insertSelective(Order record) {
        return orderMapper.insertSelective(record);
    }

    @Override
    public List<Order> getAllOrder() {
        List<Order> orders = orderMapper.getAllOrder();

        return orders;
    }

    @Override
    public List<OrderInfo> getOrderInfo(Integer uid, int[] state, int start) {

        List<Order> orders = orderMapper.getOrderInfo(uid, state, (start - 1) * 5);
        List<OrderInfo> orderInfos = new ArrayList<>();

        for (Order o : orders) {
            OrderInfo orderInfo = new OrderInfo();
            orderInfo.setOrder(o);
            orderInfo.setAddressInfo(addreasMapper.getAddressInfoById(o.getAddressId()));
            orderInfo.setOrderDetails(orderDetailMapper.selectByOrderId(o.getId()));
            orderInfos.add(orderInfo);
        }

        return orderInfos;
    }

    @Override
    public int updateStatus(Integer id, Integer uid, Integer status) {
        if (status == 0 || status == 1 || status == 2) {

            return orderMapper.updateStatus(id, uid, status);
        }

        return 0;
    }

    @Override
    public List<Order_detail> selectComment(Integer uid, Integer status) {
        List<Order_detail> details = orderDetailMapper.selectComment(uid, status);

        for (Order_detail o : details) {
            o.getGoods().setCommentNum(commentMapper.countCommentNum(o.getGoodsId()));
        }

        return details;
    }

    @Override
    public List<Order_detail> selectNoComment(Integer uid) {
        List<Order_detail> details = orderDetailMapper.selectNoComment(uid);

        for (Order_detail o : details) {
            o.getGoods().setCommentNum(commentMapper.countCommentNum(o.getGoodsId()));
        }

        return details;
    }

    @Override
    public int insertSelective(Order_detail record) {
        return orderDetailMapper.insertSelective(record);
    }

    @Override
    public Order selectByCode(String code) {
        return orderMapper.selectByCode(code);
    }

    @Override
    public Order selectByPrimaryKey(Integer id) {
        return orderMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Order_detail> selectByOrderId(Integer oid) {
        return orderDetailMapper.selectByOrderId(oid);
    }

    @Override
    public int updateByPrimaryKeySelective(Order record) {
        return orderMapper.updateByPrimaryKeySelective(record);
    }




}
